package com.appspot.jevemanager.server;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import com.appspot.jevemanager.client.SkillTreeService;
import com.appspot.jevemanager.xml.EveHandler;
import com.beimin.eveapi.character.sheet.CharacterSheetParser;
import com.beimin.eveapi.character.sheet.CharacterSheetResponse;
import com.beimin.eveapi.core.ApiAuthorization;
import com.beimin.eveapi.core.ApiException;
import com.beimin.eveapi.server.ServerStatusParser;
import com.beimin.eveapi.server.ServerStatusResponse;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class SkillTreeServiceImpl extends RemoteServiceServlet implements SkillTreeService {
	
	public SkillTreeServiceImpl() {
		
	}
	/**
	 * Auto-generated serialVersionUID
	 */
	private static final long serialVersionUID = 1997434602302711995L;
	
	private static final String BASE_URL = "https://api.eveonline.com";

	public Boolean isServerOnline() {
		Boolean ret = new Boolean("false");
		try {
		    URL server = new URL(BASE_URL + "/server/ServerStatus.xml.aspx");
		    URLConnection serverConnection = server.openConnection();
		    XMLReader reader = XMLReaderFactory.createXMLReader();
		    EveHandler handler = new EveHandler();
		    reader.setContentHandler(handler);
			reader.setErrorHandler(handler);
			reader.parse(new InputSource(serverConnection.getInputStream()));
		    //System.out.println("XML: " + xml);
		} catch (MalformedURLException e) {     // new URL() failed
		    e.printStackTrace();
		} catch (IOException e) {               // openConnection() failed
		    e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		return ret;
	}
	
	public String getOnlinePlayers() {
		String ret = "";
		try {
			ServerStatusParser parser = ServerStatusParser.getInstance();
			ServerStatusResponse response = parser.getServerStatus();
			ret = new Integer(response.getOnlinePlayers()).toString();
			System.out.println("Players: " + ret);
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return ret;
	}
	
	/*private int getCharacterId() {
		String ret = "";
		try {
			Charac parser = ServerStatusParser.getInstance();
			ServerStatusResponse response = parser.getServerStatus();
			ret = new Integer(response.getOnlinePlayers()).toString();
			System.out.println("Players: " + ret);
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	public CharacterSheetResponse getCharacterSheet() {
		ApiAuthorization auth = new ApiAuthorization(123, 456, "abc");
		CharacterSheetParser parser = CharacterSheetParser.getInstance();
		CharacterSheetResponse response = parser.getResponse(auth);
	}
	
	public void setup() throws Exception {
		context.addRoutes(new RouteBuilder() {
			@Override
			public void configure() {
				final String resPath;
				if (path != null && page != null && resourcePath == null) {
					resPath = path.getPath() + "/" + page.getPage() + ".xml.aspx";
				} else if (path == null && page == null && resourcePath != null) {
					resPath = resourcePath;
				} else {
					throw new RuntimeException("One of the construction options has been missed: path: " + path
							+ " page: " + " resourcePath: " + resourcePath);
				}

				from("jetty:" + MockApi.URL + resPath)
						.process(
							new ExchangeProcessor(
								FullAuthParserTest.this,
								path.getPath() + "/" + page.getPage() + ".xml"))
						.end();
			}
		});
		context.start();
		EveApi.setConnector(new ApiConnector(MockApi.URL));
	}*/
}
